package com.wellink.viewswitcherdemo;

import android.content.ClipData;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class SwitcherAdapter extends BaseAdapter {

    Context context;
    List<ItemData> list;

    public SwitcherAdapter(Context context, List<ItemData> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        if(MainActivity.screenNo == MainActivity.screenNo -1 &&
                list.size() % MainActivity.NUMBER_PRE_SCREEN != 0){
            return list.size() % MainActivity.NUMBER_PRE_SCREEN;
        }
        return MainActivity.NUMBER_PRE_SCREEN;
    }

    @Override
    public ItemData getItem(int position) {
        return list.get(MainActivity.screenNo * MainActivity.NUMBER_PRE_SCREEN + position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Hoder hoder;
        if(null == convertView){
            hoder = new Hoder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_layout,null);
            hoder.icon = convertView.findViewById(R.id.img);
            hoder.text = convertView.findViewById(R.id.title);
            convertView.setTag(hoder);
        }else{
            hoder = (Hoder)convertView.getTag();
        }

        hoder.icon.setImageResource(getItem(position).getIcon());
        hoder.text.setText(getItem(position).getTitle());

        return convertView;
    }

    class Hoder{
        ImageView icon;
        TextView text;
    }
}
